//
//  MaintsAcademicTableViewCell.m
//  NLProjectFrameworkDemo
//
//  Created by iosdeveloper on 2020/4/26.
//  Copyright © 2020 yj_zhang. All rights reserved.
//

#import "MaintsAcademicTableViewCell.h"

static NSInteger btnTag=222;//标记按钮tag
@interface MaintsAcademicTableViewCell()
@property(nonatomic,strong)MantisAcademicCellData*currentData;
@end

@implementation MaintsAcademicTableViewCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self setUpView];
    }
    return self;
}

-(void)reloadCellModel:(MantisCellBaseModel*)data
{
    [super reloadCellModel:data];
    MantisAcademicCellData*newData=(MantisAcademicCellData*)data;
    self.currentData=newData;
    self.cellBaseData=newData;//用于重发消息使用
    self.titlelabel.text=newData.content.additionMsg;
    [self updateView];

}

-(void)setUpView{
    [super setUpView];
   
    _titlelabel = [[UILabel alloc] init];
    _titlelabel.numberOfLines = 0;
    _titlelabel.font = KIMFontRegularSize(16);
    [self.contentView addSubview:_titlelabel];
    [_titlelabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.avaterView.mas_right).offset(adaptWidth750(20));
        make.top.mas_equalTo(self.avaterView);
        make.right.equalTo(self);
    }];
    
    UIView*btnBgView = [UIView new];
    self.btnBgView=btnBgView;
    [self.contentView addSubview:btnBgView];
   
    
}


- (void)updateView
{
   
    CGFloat btnBgWidth=IMSCREEN_WIDTH-adaptWidth750(100*2);
    
    CGFloat btnBgHeight=adaptWidth750(200);
    [self.btnBgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.titlelabel);
        make.top.equalTo(self.titlelabel.mas_bottom).offset(adaptWidth750(20));
        make.size.mas_equalTo(CGSizeMake(btnBgWidth, btnBgHeight));
    }];

    self.btnBgView.hidden=self.currentData.content.isHidden;
    NSArray<MantisTextCellSubContentData*>*arr=self.currentData.content.content;
   
    int rowNum=3;//每行排4个
   
    for (int i = 0; i < arr.count; i++)
      {
          UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
          button.backgroundColor =i==0? [UIColor redColor]:[UIColor blueColor];
          button.tag = i+btnTag;
          //确定button位置
          //3.在第几行
          int row =  i / rowNum;
          //4.在第几列
          int cloumn = i % rowNum;
          
          CGFloat w = btnBgWidth;
          CGFloat h = btnBgHeight;
          CGFloat blankWidth=12;
          CGFloat blankheight=5;
          CGFloat bw = (w-4*blankWidth)/3;
          CGFloat bh = (h-4*blankheight)/3;
          
          CGFloat x = blankWidth + (bw+blankWidth) * cloumn;
          CGFloat y = (bh+blankheight) * row+blankheight;
          
          button.frame = CGRectMake(x, y, bw, bh);
          NSLog(@"+++%@",NSStringFromCGRect(button.frame));
          [button addTarget:self action:@selector(tapFaceButton:) forControlEvents:UIControlEventTouchUpInside];
          
          [self.btnBgView addSubview:button];
          
            ViewRadius(button, 4);
          
          //刷新btn数据
           MantisTextCellSubContentData*btnData =arr[i];
          if ([NSString isBlankString:btnData.bgColor]) {
              btnData.bgColor=@"#06CB94";
          }
            button.backgroundColor =[UIColor jk_colorWithHexString:btnData.bgColor];
          [button setTitle:btnData.label forState:UIControlStateNormal];
      }

}




-(void)tapFaceButton:(UIButton*)sender{
    NSInteger index=sender.tag-btnTag;
    MantisTextCellSubContentData*btnData=self.currentData.content.content[index];
    !self.btnTitleBlock?:self.btnTitleBlock(btnData);
}





- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
